<template>
	<view class="release">
		<view class="part">
			<view class="partTit">
				货物信息
			</view>
			<view class="partVal">
				<view class="line dFlex jBetween_aCenter">
					<text>货物名称</text>
					<view class="val">
						<input v-model="transport.name" type="text" placeholder="请输入货物名称"/>
					</view>
				</view>
				<view class="line dFlex jBetween_aCenter">
					<text>承运吨数</text>
					<view class="val">
						<input v-model="transport.ton" type="number" placeholder="请输入货物吨数(限填数字)"/>
					</view>
				</view>
				<view class="line dFlex jBetween_aCenter">
					<text>出价金额</text>
					<view class="val">
						<input type="number" v-model="transport.ton_price" placeholder="请填写出价金额(每吨运费 限填数字)"/>
					</view>
				</view>
				<view class="line dFlex jBetween_aCenter">
					<text>运输车型</text>
					<view class="val">
						<picker mode="selector" :range="carType" @change="chooseCarType">
							<input type="text" disabled v-model="transport.car_type" placeholder="请选择运输车型"/>
						</picker>
					</view>
				</view>
			</view>
		</view>
		<view class="part">
			<view class="partTit">
				行程信息
			</view>
			<view class="partVal">
				<view class="line dFlex jBetween_aCenter">
					<text>发车日期</text>
					<view class="val">
						<picker mode="date" @change="(event)=>{chooseTime(event,'load_time')}">
							<input type="text" v-model='transport.load_time' disabled placeholder="请选择发车日期"/>
						</picker>
					</view>
				</view>
				<view class="line dFlex jBetween_aCenter">
					<text>发车地点</text>
					<view class="val" @click="chooseCity('loading_address')">
						<input type="text" v-model='loading_address' disabled placeholder="请选择城市"/>
					</view>
				</view>
				<view class="line dFlex jBetween_aCenter">
					<text style="visibility: hidden;">测试</text>
					<view class="val">
						<input type="text" v-model="transport.load_address1" placeholder="请输入详细地址"/>
					</view>
				</view>
				<view class="line dFlex jBetween_aCenter">
					<text>到达日期</text>
					<view class="val">
						<picker mode="date" @change="(event)=>{chooseTime(event,'unload_time')}">
							<input type="text" v-model="transport.unload_time" disabled placeholder="请选择到达日期"/>
						</picker>
					</view>
				</view>
				<view class="line dFlex jBetween_aCenter">
					<text>到达地点</text>
					<view class="val" @click="chooseCity('unloading_address')">
						<input type="text" v-model="unloading_address" disabled placeholder="请选择城市"/>
					</view>
				</view>
				<view class="line dFlex jBetween_aCenter">
					<text style="visibility: hidden;">测试</text>
					<view class="val">
						<input type="text" v-model="transport.unload_address1" placeholder="请输入详细地址"/>
					</view>
				</view>
			</view>
		</view>
		<view class="part">
			<view class="partTit">
				备注信息
			</view>
			<view class="partVal">
				<input type="textarea" v-model='transport.remark' placeholder="请输入备注信息 可留空"/>
			</view>
		</view>
		<view class="part" style="margin-bottom: 40px;">
			<view class="partTit">
				信息费
			</view>
			<view class="partVal">
				<input type="number" v-model="transport.info_money" placeholder="请填写司机需支付的信息费金额,可留空"/>
			</view>
		</view>
		<view class="btn" :style="{
			'bottom':isIos ? 'env(safe-area-inset-bottom)' : '0px'
		}" @click="saveInfo">立即发布</view>
	</view>
</template> 

<script>
		import {
			addship,shipInfo,platformInfo
		} from '@/tool/http.js';
	export default {
		data() {
			return {
				isIos:getApp().globalData.isIos,
				time:'',	//second 再次发布
				transport:{
					id:'',
					ton:'',
					ton_price:'',
					car_type:'',
					load_province1:'',
					load_city1:"",
					load_area1:'',
					load_address1:'',
					lng1:'',
					lat1:'',
					remark:"",
					unload_province1:'',
					unload_city1:"",
					unload_area1:'',
					unload_address1:'',
					load_time:'',
					unload_time:'',
					lng4:'',
					lat4:'',
					name:'',
					info_money:'',
				},
				loading_address:'',
				unloading_address:'',
				carType:[],
			};
		},
		onLoad(e) {
			this.transport.id = e.id?e.id:'';
			if(e.id){
				this.getInfo();
				this.time = e.time ? e.time : '';
			}
			this.getPlatformInfo();
		},
		methods:{
			// 选择货车类型
			chooseCarType(e){
				this.transport.car_type = this.carType[e.detail.value];
			},
			async getPlatformInfo(){
				let res = await platformInfo({});
				if(res){
					this.carType = res.car_type;
				}
			},
			// 货运信息详情
			async getInfo(){
				let res = await shipInfo({
					id:this.transport.id
				});
				if(res){
					for(var i in this.transport){
						this.transport[i] = res[i];
						/* if(i != 'load_time' && i != 'unload_time'){
							this.transport[i] = res[i];
						} */
					}
					this.loading_address = `${this.transport.load_province1}_${this.transport.load_city1}_${this.transport.load_area1}`;
					this.unloading_address = `${this.transport.unload_province1}_${this.transport.unload_city1}_${this.transport.unload_area1}`;
					console.log(this.transport);
				}
			},
			// 保存信息
			async saveInfo(){
				if(!this.loading_address){
					uni.showToast({
						title:'请选择发车城市',
						icon:"none"
					})
					return;
				}
				if(!this.transport.load_address1){
					uni.showToast({
						title:'请完善发车城市的详细地址',
						icon:"none"
					})
					return;
				}
				if(!this.unloading_address){
					uni.showToast({
						title:'请选择到达城市',
						icon:"none"
					})
					return;
				}
				if(!this.transport.unload_address1){
					uni.showToast({
						title:'请完善到达城市的详细地址',
						icon:"none"
					})
					return;
				}
				this.transport.memberId = uni.getStorageSync('uid');
				if(this.time){
					this.transport.id = '';
				}
				
				let res = await addship(this.transport);
				if(res){
					uni.showToast({
						title:'保存成功',
						icon:"success"
					})
					setTimeout(()=>{
						uni.navigateBack({delta:1})
					},1650)
				}
			},
			// 选择时间
			chooseTime(event,time){
				this.transport[time] = event.detail.value;
			},
			// 选择城市
			chooseCity(key){
				uni.chooseLocation({
					success:(locate)=>{
						if(key == "loading_address"){
							this.transport.lat1 = locate.latitude;
							this.transport.lng1 = locate.longitude;
						}else{
							this.transport.lat4 = locate.latitude;
							this.transport.lng4 = locate.longitude;
						}
						uni.request({
							url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=location',//腾讯官方逆解析接口，可直接复制
							data: {
								location: locate.latitude + ',' + locate.longitude,
								'key': getApp().globalData.mapKey,//自己申请的Key
							},
							method: "GET",
							success:(res)=>{
								res = res.data;
								if(res.status == 0){
									this[key] = res.result.address_component.province + '_' + res.result.address_component.city + '_' + res.result.address_component.district;
									if(key == "loading_address"){
										this.transport.load_province1 = res.result.address_component.province;
										this.transport.load_city1 = res.result.address_component.city;
										this.transport.load_area1 = res.result.address_component.district;
										this.transport.load_address1=locate.name;
									}else{
										this.transport.unload_province1 = res.result.address_component.province;
										this.transport.unload_city1 = res.result.address_component.city;
										this.transport.unload_area1 = res.result.address_component.district;
										this.transport.unload_address1=locate.name;
									}
								}else{
									uni.showToast({
										title:'地理位置解析失败',
										icon:"none"
									})
								}
							},
							fail: res=> {
								console.log(res.errMsg,'解析失败返回的错误信息');
							}
						})
					},
					fail:(err)=>{
						console.log(err);
						uni.showToast({
							title:'位置选取失败',
							icon:"none"
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.release{
		.btn{
			width: calc(100% - 24px);
			padding: 10px 0;
			border-radius: 4px;
			color:white;
			margin-left:12px;
			text-align: center;
			background: #0087FC;
			margin-bottom: 30px;;
		}
		.part{
			.partVal{
				.line{
					width: 100%;
					font-size: 12px;
					line-height: 32px;
					color: #666666;
					.val{
						input{
							text-align: right;
						}
						flex:1;
						text-align: right;
						margin-left: 8px;
						font-size: 14px;
					}
				}
				width: 100%;
				padding: 12px;
				background: white;
				border-radius: 4px;
				box-sizing: border-box;
			}
			.partTit{
				font-size: 12px;
				color: #555555;
				margin-bottom: 8px;
			}
			width: 100%;
			margin-bottom: 12px;
		}
		width: 100%;
		
		position: relative;
		padding: 12px;
		box-sizing: border-box;
	}
</style>
